<?php

namespace App\Exports;

use App\Models\Waybill;
// use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
// use Maatwebsite\Excel\Concerns\ShouldAutoSize; // 下载湿垃圾汇总月报
use Maatwebsite\Excel\Concerns\WithEvents;     // 自动注册事件监听器
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;    // 导出 0 原样显示，不为 null
use Maatwebsite\Excel\Concerns\WithTitle;    // 设置工作䈬名称
use Maatwebsite\Excel\Events\AfterSheet;    // 在工作表流程结束时会引发事件
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use Maatwebsite\Excel\DefaultValueBinder;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;

class WaybillExport extends DefaultValueBinder implements WithCustomValueBinder, WithHeadings, FromCollection, WithEvents, WithStrictNullComparison
{
    use Exportable;

    // 要导出的数据
    private $data;

    // 总行数
    public $rowNum;

    public function __construct($data, $title)
    {
        $this->data = $data;

        $this->title = $title;
    }

    // 设置表头
    public function headings() : array
    {
        $head = [
            'order_no' =>'淘宝订单',
            'express_no' =>'运单号',
            'part_bn' =>'料号',
            'goods_name' =>'商品名称',
            'goods_bn' =>'商品编号',
            'goods_num' =>'商品数量',
            'consignee_name' => '收货人姓名',
            'consignee_address' => '详细地址',
            'supplier_name' => '供应商账号名',
            'seller_name' =>'店铺卖家',
            'declare_status' => '清单申报状态',
            'platform_delivery_status' => '平台发货状态',
            'warehouse_delivery_status' => '仓库发货状态',
            'clearance_type' => '清关类型',
            // 'goods_price' => '商品单价',
            'status' => '运单状态',
            'order_amount' =>'订单总价',
            'goods_price' =>'合作价',
            'cost_amount' => '成本价',
            'profit_amount' => '利润',
            'created_at' => '运单时间'
        ];

        return $head;
    }

    public function title(): string
    {
        // 设置工作䈬的名称
       return $this->title;
    }

     /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $data = $this->data;

        $platform_delivery_arr = ['0' => '订单取消', '1' => '淘分销已发货!'];

        $warehouse_delivery_arr = ['0'=>'订单取消成功', '1'=>'下单中,等待回执'];

        $list[] = [];
        foreach ($data as $key => $value) {
            $list[] = [
                'order_no' => trim($value['order_no']),
                'express_no' => trim($value['express_no']),
                'part_bn' => trim($value['part_bn']),
                'goods_name' => trim($value['goods_name']),
                'goods_bn' => trim($value['goods_bn']),
                'goods_num' => trim($value['goods_num']),
                'consignee_name' => trim($value['consignee_name']),
                'consignee_address' => trim($value['consignee_address']),
                'supplier_name' => trim($value['supplier_name']),
                'seller_name' => trim($value['seller_name']),
                'declare_status' => $value['declare_status']==0 ? '待申报' : '申报完成',
                'platform_delivery_status' => $platform_delivery_arr[$value['platform_delivery_status']],
                'warehouse_delivery_status' => $warehouse_delivery_arr[$value['warehouse_delivery_status']],
                'clearance_type' => trim($value['clearance_type']),
                // 'goods_price' => trim($value['goods_price']),
                'status' => $value['status']==1 ? '已计算' : '未计算',
                'order_amount' => trim($value['order_amount']),
                'goods_price' => trim($value['goods_price']),
                'cost_amount' => trim($value['cost_amount']),
                'profit_amount' => trim($value['profit_amount']),
                'created_at' => trim($value['created_at'])
            ];
        }

        // 订单总额
        $order_amount_arr = array_column($list, 'order_amount');

        // 总成本
        $cost_amount_arr = array_column($list, 'cost_amount');

        // 总利润
        $profit_amount_arr = array_column($list, 'profit_amount');

        // 合作价
        $goods_price = array_column($list, 'goods_price');

        $list[] = [
            'order_no' => '',
            'express_no' => '',
            'part_bn' => '',
            'goods_name' => '',
            'goods_bn' => '',
            'goods_num' => '',
            'consignee_name' => '',
            'consignee_address' => '',
            'supplier_name' => '',
            'seller_name' => '',
            'declare_status' => '',
            'platform_delivery_status' => '',
            'warehouse_delivery_status' => '',
            'clearance_type' => '',
            // 'goods_price' => '',
            'status' => '总计：',
            'order_amount' => array_sum($order_amount_arr),
            'goods_price' => array_sum($goods_price),
            'cost_amount' => array_sum($cost_amount_arr),
            'profit_amount' => array_sum($profit_amount_arr)
        ];

        // array_unshift($list, $header);
        $this->rowNum = count($list);

        $result = collect($list);
        // dd('aa',$result);
        return $result;
    }

    /**
     * 设置列单元格格式
     */
    public function columnFormats(): array
    {
        // TODO: Implement columnFormats() method.
        return [
            //'C' => NumberFormat::FORMAT_NUMBER_00, //重量保留两位小数
        ];
    }

    /**
     * registerEvents.
     * 事件监听
     * @return array
     */
    public function registerEvents(): array
    {
        return [];
    }

    public function bindValue(Cell $cell, $value)
    {
        $column = $cell->getColumn();
        if (in_array( $column, ['A', 'E'])) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
